"use strict";

var C = require('../common'),
    https = require('https'),
    fs = C.fs;

module.exports = {

    type: 'server',

    load: function (owner, httpsConfig, cb) {

        httpsConfig.sslKey || C.throwRequired('[' + owner.stack + '].https.sslKey');
        httpsConfig.sslCert || C.throwRequired('[' + owner.stack + '].https.sslCert');

        var privateKey  = fs.readFileSync(httpsConfig.sslKey, 'utf8');
        var certificate = fs.readFileSync(httpsConfig.sslCert, 'utf8');
        var credentials = {key: privateKey, cert: certificate};

        var port = httpsConfig.port || 3001;

        https.createServer(credentials, owner.app.engine).listen(port, function(err) {
            if (err) return cb(err);

            console.log('Https server is listening on port [' + port + '].');
            cb();
        });
    }
};